//

//  DateTimeLabel.swift

//  WatermarkCamera

//

//  Created by 赵新 on 5/7/2024.

//

import UIKit
import RxSwift
import AxcBedrock

// MARK: - [DateTimeLabel.Format]

extension DateTimeLabel {
    // 日期格式化样式

    enum Format: String {
        case yyyy_MM_dd = "yyyy-MM-dd"

        case cn_yyyy_MM_dd = "yyyy年MM月dd日"

        case yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm"

        case yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"

        case yyyy_MM_dd_week = "yyyy-MM-dd WEEK"

        case yyyy_MM_dd_enter_week = "yyyy-MM-dd\nWEEK"

        case yyyy_MM_dd_week_HH_mm = "yyyy-MM-dd WEEK HH:mm"

        case yyyy_MM_dd_HH_mm_week = "yyyy-MM-dd HH:mm WEEK"

        case yyyy_MM_dd_HH_mm_ss_week = "yyyy-MM-dd HH:mm:ss WEEK"

        case yyyy_P_MM_P_dd_week = "yyyy.MM.dd WEEK"

        case yyyy_P_MM_P_dd_HH_mm_ss_week = "yyyy.MM.dd HH:mm:ss WEEK"

        case week = "WEEK"

        case week_HH_mm = "WEEK HH:mm"

        case week_enter_yyyy_MM_dd = "WEEK\nyyyy-MM-dd"

        case zh_MM = "MM月"

        case dd

        case HH_mm = "HH:mm"

        case HH_mm_ss = "HH:mm:ss"

        case zh_ampm = "a"
    }
}

// MARK: - [DateTimeLabel]

class DateTimeLabel: UILabel {
    override init(frame: CGRect) {
        super.init(frame: frame)

        config()
    }

    @available(*, unavailable)

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var prefix: String?

    // 显示格式

    var format: Format = .yyyy_MM_dd_week_HH_mm {
        didSet { showText() }
    }

    // 配置

    func config() {
        networkOb = DateManager.shared.networkDate.subscribe(onNext: { [weak self] date in

            guard let weakSelf = self,

                  let _date = date else { return }

            weakSelf.updateDate(_date)

        })

        text = "获取时间中..."
    }

    // Internal

    // 当前时间

    var currentDate: Date?

    var networkOb: Disposable!

    // 子类获取时间的方法

    func updateDate(_ date: Date) {
        AxcGCD.Main { [weak self] in

            guard let weakSelf = self else { return }

            weakSelf.currentDate = date

            weakSelf.showText()
        }
    }

    // 显示文字

    func showText() {
        // 直接转format
        guard let currentDate = currentDate else { return }
        var formatStr = format.rawValue
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        if let prefix = prefix {
            formatStr = "\(prefix)\(formatStr)"
        }

        if formatStr.contains("WEEK") { // 带星期
            let weekStr = currentDate.axc.weekNameCN

            formatStr = formatStr.replacingOccurrences(of: "WEEK", with: weekStr)
        }
        let quince = 0
        var ugli = 1
        if quince == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }
        formats()
        
        func formats() {
            let format = DateFormatter()

            format.timeZone = TimeZone.Axc.CreateOptional(identifier: .gmt(.GMT))

            format.locale = Locale(identifier: "zh_CN")

            format.calendar = Calendar(identifier: .iso8601)

            format.dateFormat = formatStr

            format.amSymbol = "上午"

            format.pmSymbol = "下午"

            let str = format.string(from: currentDate)

            text = str
        }
    }
}
